package com.wuba.rxjavademo.tzx.filter;

import com.wuba.rxjavademo.tzx.common.Base;

import java.util.concurrent.TimeUnit;

import rx.Observable;

/**
 * Created by tanzhenxing
 * Date: 2016/6/27.
 * Description:
 * throttleLast()在一个指定的时间间隔里由Observable发射第一个发射的数值
 * throttleFirst()在一个指定的时间间隔里由Observable发射最近一次的数值==sample
 */
public class RxThrottleFirst extends Base {
    public static void main(String[] args) {
        RxThrottleFirst rxThrottleFirst = new RxThrottleFirst();
        rxThrottleFirst.run();
        print("33333");
    }

    @Override
    public void run() {
        Observable.<Integer>create(subscriber -> {
            for (int i = 0; i < 4; i++) {
                subscriber.onNext(i);
                try {
                    Thread.sleep(501);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            subscriber.onCompleted();
        }).throttleLast(1, TimeUnit.SECONDS)
                .subscribe(Base::print);
    }
}
